<?php

/**
 * comprobante actions.
 *
 * @package    sf_sandbox
 * @subpackage comprobante
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class comprobanteActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->forward404Unless($this->unidad_funcional = Doctrine_Core::getTable('UnidadFuncional')->find(array("2")), sprintf('Object unidad_funcional does not exist (%s).', "1"));
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new ComprobanteForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new ComprobanteForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($comprobante = Doctrine_Core::getTable('Comprobante')->find(array($request->getParameter('id'))), sprintf('Object comprobante does not exist (%s).', $request->getParameter('id')));
        $this->form = new ComprobanteForm($comprobante);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($comprobante = Doctrine_Core::getTable('Comprobante')->find(array($request->getParameter('id'))), sprintf('Object comprobante does not exist (%s).', $request->getParameter('id')));
        $this->form = new ComprobanteForm($comprobante);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($comprobante = Doctrine_Core::getTable('Comprobante')->find(array($request->getParameter('id'))), sprintf('Object comprobante does not exist (%s).', $request->getParameter('id')));
        $comprobante->delete();

        $this->redirect('comprobante/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $comprobante = $form->save();

            $this->redirect('comprobante/edit?id=' . $comprobante->getId());
        }
    }

    protected function getPager($q, $page, $model) {
        $pager = new sfDoctrinePager($model, sfConfig::get('app_nro_x_pagina_contribuyente'));
        $pager->setQuery($q);
        $pager->setPage($page);
        $pager->init();

        return $pager;
    }

    public function executeFiltro(sfWebRequest $request) {
        $comprobante = ComprobanteTable::getUltimo();
        if (count($comprobante) > 0) {
            $this->nrocomprobante = $comprobante['nro_comprobante'] + 1;
        } else {
            $this->nrocomprobante = 1;
        }
        $this->comprobantes = ComprobanteTable::getComprobantesImpagos(0);
    }

    public function executeFiltrarUnidadesFuncionales(sfWebRequest $request) {
        if ($request->isMethod('post')) {
            $dato = $request->getPostParameter("dato");
        } else {
            $dato = $request->getGetParameter("dato");
        }

        $this->unidadesFuncionales = $this->getPager(UnidadFuncionalTable::getBuscarUnidadesPopUp($dato), $request->getGetParameter('page'), "UnidadFuncional");
        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial("comprobante/resultadoUnidadesFuncionales", array('unidadesFuncionales' => $this->unidadesFuncionales));
        }
    }

    public function executeBuscar(sfWebRequest $request) {
        $this->setLayout("busqueda");
    }

    public function executeBuscarcomprobantesImpagos(sfWebRequest $request) {
        if ($request->isMethod('post')) {
            $iduf = $request->getPostParameter("id");
        } else {
            $iduf = $request->getGetParameter("id");
        }
        $this->comprobantes = ComprobanteTable::getComprobantesImpagos($iduf);
    }
    public function executeBuscarComprobantes(sfWebRequest $request) {
        if ($request->isMethod('post')) {
            $iduf = $request->getPostParameter("id");
        } else {
            $iduf = $request->getGetParameter("id");
        }
      
        $this->comprobantes = ComprobanteTable::getComprobantes($iduf);
    }
    public function executeRegistrarPago(sfWebRequest $request) {

        $cbtes = $request->getPostParameter('cbx[]');
        try {
            $pago = new Pago();
            $pago->setFormaPagoId(1);
            $pago->setFechahora(date('Y-m-d H:i:s'));
            $pago->save();
            
            foreach ($cbtes as $c) {
                $compobante = Doctrine_Core::getTable('Comprobante')->find(array($c));
                $pago_linea = new PagoLinea();
                $pago_linea->setPagoId($pago->getId());
                $pago_linea->setComprobanteId($c);
                $pago_linea->setMora($compobante->getMora());
                $pago_linea->save();
            }
            $this->getUser()->setFlash('notice', "La factura se registró con el número: " . $nrofo);
        } catch (Exception $e) {
            $this->getUser()->setFlash('error', $e->getMessage());
        }
        $this->redirect("comprobante/filtro");
    }
 public function executeImprimir(sfWebRequest $request){
     if ($request->isMethod('post')) {
            $id = $request->getPostParameter("id");
        } else {
            $id = $request->getGetParameter("id");
        }
      
        $comprobante =  Doctrine_Core::getTable('Comprobante')->find(array(16));
        $itemsconceptos =array();
        $lectura = array();
        $conceptosfijos = array();
//        if(count( )>0)
      
//            $itemsconceptos=$comprobante->getLineaComprobante();
           $nro_comprobante=  $comprobante->getNroComprobante();
           $unidad_funcional =  $comprobante->getUnidadFuncional();
          
              if ( $comprobante->getTipoComprobante()->getAbreviatura()=='FC'){  
                   $lectura=$comprobante->getLetura();
              }
        $html = $this->getPartial('facturaconsumo/factura', array('unidadfuncional' => $unidad_funcional, 'conceptos' => $itemsconceptos, 'lectura' => $lectura, 'conceptosfijos' => $conceptosfijos));
        $config = sfTCPDFPluginConfigHandler::loadConfig();

        // pdf object
        $pdf = new sfTCPDF();

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('NORDSOFT');
        //$pdf->SetTitle('Planilla Relevamiento de Lecturas');
        // set default header data
        $pdf->SetHeaderData("", "", PDF_HEADER_TITLE, "Servicio de administración y provisión de agua potable");

        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, 25, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        //set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // ---------------------------------------------------------
        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('dejavusans', '', 14, '', true);

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

        // Set some content to print
        // Print text using writeHTMLCell()
        $pdf->writeHTMLCell($w = 0, $h = 0, $x = '', $y = '', $html, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = '', $autopadding = true);

        // ---------------------------------------------------------
        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('example_001.pdf', 'I');

        // Stop symfony process

        throw new sfStopException();
       
    }
}
